/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package org.eclipse.iota.model;

import java.util.Collection;

import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.EObjectImpl;

import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;

import org.eclipse.iota.IotaPackage;

/**
 * @see org.eclipse.iota.IotaPackage#getMultiOperation()
 * @model kind="class" abstract="true"
 * @generated
 */
public abstract class MultiOperation extends EObjectImpl implements Operation {
	
	/**
	 * Alternative constructor.
	 * @generated NOT
	 * @param operands Operands.
	 */
	public MultiOperation(Expression... operands) {
		super();
		for (int i=0; i<operands.length; i++) {
			getOperands().add(operands[i]);
		}
	}
	
	/**
	 * Create a new instance of Constant which is the neutral
	 * element for this multi-operation. This always returns
	 * a fresh constant with a fixed value.
	 * @return The neutral element.
	 */
	public abstract Constant createNeutralElement();
	
	/**
	 * Check if this multi-operation has no operands.
	 * @return <code>true</code> if it is empty.
	 */
	public boolean isEmpty() {
		return getOperands().isEmpty();
	}
	
	/**
	 * Check if this multi-operation has exactly one operand.
	 * @return <code>true</code> if it has just one operand.
	 */
	public boolean isSingleton() {
		return getOperands().size()==1;
	}

	/**
	 * Get the equation that owns this expression.
	 * @generated NOT
	 */
	public Equation basicGetEquation() {
		if (eContainer() instanceof Equation) return (Equation) eContainer();
		if (eContainer() instanceof Expression) return ((Expression) eContainer()).getEquation();
		return null;
	}
	
	/**
	 * Generate a string-list representation of this multi-operation.
	 * @param separator Separator string.
	 * @return String representation (without surrounding brackets).
	 * @generated NOT
	 */
	protected String asList(String separator) {
		String result = "";
		for (int i=0; i<getOperands().size(); i++) {
			result = result + getOperands().get(i);
			if (i<getOperands().size()-1) result = result + separator;
		}
		return result;
	}
	
	
	/* ---------------------------------------------------------------- *
	 * GENERATED CODE.                                                  *
	 * Do not edit below this line. If you need to edit, move it above  *
	 * this line and change the '@generated'-tag to '@generated NOT'.   *
	 * ---------------------------------------------------------------- */	

	/**
	 * The cached value of the '{@link #getOperands() <em>Operands</em>}' containment reference list.
	 * @see #getOperands()
	 * @generated
	 * @ordered
	 */
	protected EList<Expression> operands;

	/**
	 * @generated
	 */
	public MultiOperation() {
		super();
	}

	/**
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return IotaPackage.Literals.MULTI_OPERATION;
	}

	/**
	 * Returns the value of the '<em><b>Equation</b></em>' reference.
	 * @return the value of the '<em>Equation</em>' reference.
	 * @see org.eclipse.iota.IotaPackage#getExpression_Equation()
	 * @model transient="true" changeable="false" volatile="true" derived="true"
	 * @generated
	 */
	public Equation getEquation() {
		Equation equation = basicGetEquation();
		return equation != null && equation.eIsProxy() ? (Equation)eResolveProxy((InternalEObject)equation) : equation;
	}
	
	/**
	 * Returns the value of the '<em><b>Operands</b></em>' containment reference list.
	 * The list contents are of type {@link org.eclipse.iota.model.Expression}.
	 * @return the value of the '<em>Operands</em>' containment reference list.
	 * @see org.eclipse.iota.IotaPackage#getMultiOperation_Operands()
	 * @model containment="true"
	 * @generated
	 */
	public EList<Expression> getOperands() {
		if (operands == null) {
			operands = new EObjectContainmentEList<Expression>(Expression.class, this, IotaPackage.MULTI_OPERATION__OPERANDS);
		}
		return operands;
	}

	/**
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case IotaPackage.MULTI_OPERATION__OPERANDS:
				return ((InternalEList<?>)getOperands()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case IotaPackage.MULTI_OPERATION__EQUATION:
				if (resolve) return getEquation();
				return basicGetEquation();
			case IotaPackage.MULTI_OPERATION__OPERANDS:
				return getOperands();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case IotaPackage.MULTI_OPERATION__OPERANDS:
				getOperands().clear();
				getOperands().addAll((Collection<? extends Expression>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case IotaPackage.MULTI_OPERATION__OPERANDS:
				getOperands().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case IotaPackage.MULTI_OPERATION__EQUATION:
				return basicGetEquation() != null;
			case IotaPackage.MULTI_OPERATION__OPERANDS:
				return operands != null && !operands.isEmpty();
		}
		return super.eIsSet(featureID);
	}

}
